
import { Controller, Inject, Get, Query, Param, Del, Post, Put, Body } from '@midwayjs/core';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@midwayjs/swagger';
import { Auth } from "../decorator/auth.decorator";
import { BusinessType, Log } from "../decorator/log.decorator";
import { GradeService } from '@service/grade.service';
import { CreateGradeDto, UpdateGradeDto, QueryGradeDto} from '@dto/grade.dto';

@ApiTags('年级管理')
@Controller('system/grade')
export class GradeController {
  @Inject()
  gradeService: GradeService;
  // 创建
  @ApiOperation({ summary: '创建年级信息', description: '新增年级信息记录' })
  @ApiBody({ type: CreateGradeDto, description: '年级信息创建数据' })
  @ApiResponse({ status: 200, description: '创建成功' })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @Auth('system:grade:add')
  @Log({ title: '年级信息表-创建', businessType: BusinessType.ADD })
  @Post('/')
  async create(@Body() body: CreateGradeDto) {
    return await this.gradeService.create(body);
  }
  // 获取列表
  @ApiOperation({ summary: '获取年级列表', description: '分页查询年级信息列表' })
  @ApiResponse({ status: 200, description: '查询成功', type: 'object' })
  @Auth('system:grade:query')
  @Log({ title: '年级信息表-列表', businessType: BusinessType.LIST })
  @Get('/list')
  async list(@Query() query: QueryGradeDto) {
    return await this.gradeService.list(query);
  }
  // 获取详情
  @ApiOperation({ summary: '获取年级详情', description: '根据年级ID获取年级详细信息' })
  @ApiParam({ name: 'gradeId', description: '年级ID', type: 'string' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @ApiResponse({ status: 404, description: '年级不存在' })
  @Auth('system:grade:query')
  @Log({ title: '年级信息表-详情', businessType: BusinessType.DETAIL })
  @Get('/:gradeId')
  async detail(@Param('gradeId') gradeId: string) {
    return await this.gradeService.detail(gradeId);
  }
  // 修改
  @ApiOperation({ summary: '修改年级信息', description: '更新年级信息记录' })
  @ApiBody({ type: UpdateGradeDto, description: '年级信息更新数据' })
  @ApiResponse({ status: 200, description: '修改成功' })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 404, description: '年级不存在' })
  @Auth('system:grade:edit')
  @Log({ title: '年级信息表-修改', businessType: BusinessType.EDIT })
  @Put('/')
  async update(@Body() body: UpdateGradeDto) {
    return await this.gradeService.update(body);
  }
  // 删除
  @ApiOperation({ summary: '删除年级信息', description: '根据年级ID删除年级信息（支持批量删除）' })
  @ApiParam({ name: 'gradeId', description: '年级ID，多个ID用逗号分隔', type: 'string' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '年级不存在' })
  @Auth('system:grade:remove')
  @Log({ title: '年级信息表-删除', businessType: BusinessType.REMOVE })
  @Del('/:gradeId')
  async remove(@Param('gradeId') gradeId: string) {
    const gradeIds = gradeId.split(',').map((gradeId) => +gradeId);
    return this.gradeService.remove(gradeIds);
  }
  // 导出
  @ApiOperation({ summary: '导出年级信息', description: '导出年级信息到Excel文件' })
  @ApiBody({ type: QueryGradeDto, description: '导出查询条件' })
  @ApiResponse({ status: 200, description: '导出成功' })
  @Auth('system:grade:export')
  @Log({ title: '年级信息表-导出', businessType: BusinessType.EXPORT })
  @Post('/export')
  async export(@Body() queryParams: QueryGradeDto) {
    return await this.gradeService.export(queryParams);
  }
  //学院年级树
  @ApiOperation({ summary: '获取学院年级树', description: '获取所有学院的年级树结构' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Log({ title: '年级信息表-学院年级树', businessType: BusinessType.LIST })
  @Get('/tree')
  async tree() {
    return await this.gradeService.tree();
  }
}
